package com.xiaoyi.pdocrlibrary;

import android.content.Context;
import android.graphics.Bitmap;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class OCRPredictorNative {
    private static final AtomicBoolean isSOLoaded = new AtomicBoolean();
    private Config config;
    private long nativePointer = 0;

    public static class Config {
        public String clsModelFilename;
        public String cpuPower;
        public int cpuThreadNum;
        public String detModelFilename;
        public String recModelFilename;
    }

    /* access modifiers changed from: protected */
    public native float[] forward(long j, float[] fArr, float[] fArr2, Bitmap bitmap);

    /* access modifiers changed from: protected */
    public native long init(Context context, String str, String str2, String str3, int i, String str4);

    /* access modifiers changed from: protected */
    public native void release(long j);

    public static void loadLibrary() throws RuntimeException {
        AtomicBoolean atomicBoolean = isSOLoaded;
        if (!atomicBoolean.get() && atomicBoolean.compareAndSet(false, true)) {
            try {
                System.loadLibrary("Native");
            } catch (Throwable th) {
                throw new RuntimeException("Load libNative.so failed, please check it exists in apk file.", th);
            }
        }
    }

    public OCRPredictorNative(Context context, Config config2) {
        this.config = config2;
        loadLibrary();
        this.nativePointer = init(context, config2.detModelFilename, config2.recModelFilename, config2.clsModelFilename, config2.cpuThreadNum, config2.cpuPower);
    }

    public ArrayList<OcrResultModel> runImage(float[] fArr, int i, int i2, int i3, Bitmap bitmap) {
        return postprocess(forward(this.nativePointer, fArr, new float[]{1.0f, (float) i3, (float) i2, (float) i}, bitmap));
    }

    public void destory() {
        long j = this.nativePointer;
        if (j > 0) {
            release(j);
            this.nativePointer = 0;
        }
    }

    private ArrayList<OcrResultModel> postprocess(float[] fArr) {
        ArrayList<OcrResultModel> arrayList = new ArrayList<>();
        int i = 0;
        while (i < fArr.length) {
            int round = Math.round(fArr[i]);
            int round2 = Math.round(fArr[i + 1]);
            OcrResultModel parse = parse(fArr, i + 2, round, round2);
            i += (round * 2) + 3 + round2;
            arrayList.add(parse);
        }
        return arrayList;
    }

    private OcrResultModel parse(float[] fArr, int i, int i2, int i3) {
        OcrResultModel ocrResultModel = new OcrResultModel();
        ocrResultModel.setConfidence(fArr[i]);
        int i4 = i + 1;
        for (int i5 = 0; i5 < i2; i5++) {
            int i6 = (i5 * 2) + i4;
            ocrResultModel.addPoints(Math.round(fArr[i6]), Math.round(fArr[i6 + 1]));
        }
        int i7 = i4 + (i2 * 2);
        for (int i8 = 0; i8 < i3; i8++) {
            ocrResultModel.addWordIndex(Math.round(fArr[i7 + i8]));
        }
        return ocrResultModel;
    }
}
